<?php
namespace Application\Service;

use Zend\Config\Reader\Xml;

class UpdateService
{


    public function processSingleComment($comment, $loggedinUserId)
    {
        if ($comment) {
            if ($loggedinUserId == $comment->comment_user_id) {
                return array(
                    'id' => $comment->c_id,
                    'comment' => $comment->comment,
                    'commentor' => 'You',
                	'c_image_id' => $comment->c_image_id,
                    'uid' => $comment->comment_user_id,
                    'parentId' => $comment->upid,
                    'mine' => true,
                    'date' => date('d F h:i', strtotime($comment->comment_date))
                );
            } else {
                return array(
                    'id' => $comment->c_id,
                    'comment' => $comment->comment,
                    'uid' => $comment->comment_user_id,
                    'commentor' => $comment->commentor,
                    'parentId' => $comment->upid,
                    'c_image_id' => $comment->c_image_id,
                    'date' => date('d F h:i', strtotime($comment->comment_date))
                );
            }
        }
    }

    public function processUpdate($result, $loggedinUserId)
    {
        $id = 0;
        $lastItem = null;
        $finalResult = array();
        foreach ($result as $singleResult) {
            if ($singleResult->upid != $id) {
                $id = $singleResult->upid;
                $lastItem = $singleResult;
                if ($loggedinUserId == $singleResult->uid) {
                    $singleResult['name'] = 'You';
                    $singleResult['mine'] = true;
                }
                $singleResult->date = date('d F h:i', strtotime($singleResult->date));
                if($singleResult->fileUpload != null){
                    $singleResult->fileUpload = $this->processExtraData($singleResult->fileUpload);
                }
                array_push($finalResult, $singleResult);
                if ($singleResult->comment != null) {
                    $singleResult['comments'] = array();
                    array_push($singleResult['comments'], $this->processSingleComment($singleResult, $loggedinUserId));
                }
            } else {
                array_push($lastItem['comments'], $this->processSingleComment($singleResult, $loggedinUserId));
            }
        }
        return $finalResult;
    }


    public function processExtraData($xmlData){
        $reader = new Xml();
        $data  = $reader->fromString($xmlData);
        return $data;
    }

    public function encodeExtraDataToXmlDiscussion($discussionTitle){
        $discussion = array();
        $discussion['title'] = $discussionTitle;
        $xml =  $xmlw->processUserXml($discussion. 'discussion');
        return $xml;
    }

    public function encodeFileUpload(){

    }

    public function constantUpdateData(){
        /*
         * $data = array(
         * array(
         * update => '...Hey people how are you. No update..',
         * updater => 'You',
         * date => date('d F h:i'),
         * comments => array(
         * array(
         * comment => "ISRO's success in putting the satellite in the orbit of Mars is a tribute to our ability if we set our mind to it and work together.
         *
         * Kudos to the scientists!
         *
         * That India - after US, Russia & European Union - is only the fourth successful space power in achieving this is a source of pride for all of us.
         *
         * This success reminds us again of our huge potential waiting to be tapped.
         *
         * Why can’t we build infrastructure and ensure growth and education?
         *
         * We need unity of purpose and relentless pursuit of what matters to make all Indians realize their potential. MOM inspires us all.
         *
         * Lust for power should give way to quest for excellence;
         * Divisions must pave way to desire for collective success.
         * Private greed must yield to public good.",
         * date => date('d F h:i'),
         * commentor => 'Ranjeet'
         * ),
         * array(
         * comment => 'welcome back bhai',
         * date => date('d F h:i'),
         * commentor => 'Manjeet'
         * ),
         * array(
         * comment => 'welcome back bandhu',
         * date => date('d F h:im'),
         * commentor => 'Hanjeet'
         * ),
         * array(
         * comment => 'welcome back sahab',
         * date => date('d F h:i'),
         * commentor => 'Khanjeet'
         * )
         * )
         * ),
         * array(
         * agenda => 'SB Admin 2 Bootstrap admin theme, dashboard template, or webapp UI starter. The theme features a variety of custom jQuery plugins to add extended functionality past the built in Bootstrap UI features.',
         * title => 'Can we meet on sunday on 1:00 clock..',
         * creator => 'Pankaj',
         * date => date('d F h:i'),
         * summary => array(
         * totalResponse => 20,
         * result => array(
         * array(
         * response => 3,
         * val => 'yes'
         * ),
         * array(
         * response => 7,
         * val => 'no'
         * ),
         * array(
         * response => 10,
         * val => 'don not have idea'
         * )
         * )
         * ),
         * meResponded => array(
         * answer => 'no',
         * comment => 'I dont like it'
         * ),
         * responses => array(
         *
         * array(
         * answer => 'no',
         * commentor => 'pankaj ',
         * comment => 'I dont like it'
         * )
         * ),
         * options => array(
         * array(
         * title => 'You are willing',
         * value => 'yes',
         * type => 'radio'
         * ),
         * array(
         * title => 'You are not willing',
         * value => 'no',
         * type => 'radio'
         * )
         * )
         * ),
         * array(
         * agenda => 'SB Admin 2 Bootstrap admin theme, dashboard template, or webapp UI starter. The theme features a variety of custom jQuery plugins to add extended functionality past the built in Bootstrap UI features.',
         * title => 'Can we meet on sunday on 2:00 clock..',
         * creator => 'Pankaj',
         * date => date('d F h:i'),
         * responses => array(
         * array(
         * answer => 'yes',
         * commentor => 'pankaj ',
         * comment => 'I like it'
         * ),
         * array(
         * answer => 'no',
         * commentor => 'swati ',
         * comment => 'I dont like it'
         * )
         * ),
         * options => array(
         * array(
         * title => 'You are willing',
         * value => 'yes',
         * type => 'radio'
         * ),
         * array(
         * title => 'You are not willing',
         * value => 'no',
         * type => 'radio'
         * )
         * )
         * ),
         * array(
         * update => 'Hey people how are you. No update..',
         * updater => 'Rajeev',
         * date => date('d F h:i'),
         * comments => array(
         * array(
         * comment => 'welcome back jee',
         * date => date('d F h:i'),
         * commentor => 'Ranjeet'
         * ),
         * array(
         * comment => 'welcome back bhai',
         * date => date('d F h:i'),
         * commentor => 'Manjeet'
         * ),
         * array(
         * comment => 'welcome back bandhu',
         * date => date('d F h:im'),
         * commentor => 'Hanjeet'
         * ),
         * array(
         * comment => 'welcome back sahab',
         * date => date('d F h:i'),
         * commentor => 'Khanjeet'
         * )
         * )
         * )
         * );
         */
    }
}

?>